/**
 * 初始化整体流量图标
 */
define(function(){
    var ec=require('echarts');
    require('echarts/chart/line');
    require('echarts/chart/bar');
    var zrnumber = require('echarts/util/number');

    var myChart = ec.init(document.getElementById('readDown_chart')); 

   var option_day = {
        tooltip : {
            trigger: 'axis'
        },
        legend: {
            selectedMode:true,
            data:[
                '简历投递总量','简历阅读总量','简历下载总量','',
                '主动投递简历量','主动投递简历阅读量','主动投递简历下载量'           
            ]
        },
        toolbox: {
            show : true,
            orient:'vertical',
            feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false},
                magicType : {show: true, type: ['line', 'bar']},
                restore : {show: true},
                saveAsImage : {show: true}
            }
        },
        calculable : true,
        grid: {y: 70, y2:50},
        dataZoom: {
            show: true,
            height: 20,
            realtime : true,
            start: 90
        }, 
        xAxis : [
            {
                type : 'category',
                data : ['Line','Bar','Scatter','K','Map']
            },
            {
                type : 'category',
                axisLine: {show:false},
                axisTick: {show:false},
                axisLabel: {show:false},
                splitArea: {show:false},
                splitLine: {show:false},
                data : ['Line','Bar','Scatter','K','Map']
            }
        ],
        yAxis : [
            {
                type : 'value'
            }
        ],
        series : [
            {
                name:'简历投递总量',
                type:'bar',
                xAxisIndex:1,
                itemStyle: {normal: {color:'rgba(193,35,43,0.5)', label:{show:true,formatter:function(p){return p.value > 0 ? (zrnumber.addCommas(p.value) +'\n'):'';}}}},                
                data:[40,155,95,75, 0]
            },
            {
                name:'主动投递简历量',
                type:'bar',
                
                itemStyle: {normal: {color:'rgba(193,35,43,1)', label:{show:true}}},
                data:[96,224,164,124,0]
            },
            {
                name:'简历阅读总量',
                type:'bar',
                xAxisIndex:1,
                itemStyle: {normal: {color:'rgba(181,195,52,0.5)', label:{show:true,formatter:function(p){return p.value > 0 ? (zrnumber.addCommas(p.value) +'\n'):'';}}}},
                data:[100,200,105,100,156]
            },
            {
                name:'主动投递简历阅读量',
                type:'bar',
                               
                itemStyle: {normal: {color:'rgba(181,195,52,1)', label:{show:true,textStyle:{color:'#27727B'}}}},                
                data:[491,2035,389,955,347]
            },
            {
                name:'简历下载总量',
                type:'bar',
                xAxisIndex:1,
                itemStyle: {normal: {color:'rgba(252,206,16,0.5)', label:{show:true,formatter:function(p){return p.value > 0 ? (zrnumber.addCommas(p.value) +'\n'):'';}}}},
                data:[906,911,908,778,0]
            },
            {
                name:'主动投递简历下载量',
                type:'bar',
                
                itemStyle: {normal: {color:'rgba(252,206,16,1)', label:{show:true,textStyle:{color:'#E87C25'}}}},                
                data:[3000,3000,2817,3000,0]
            }
        ]
    };


    // var fieldMap = {"sum_delivery":0, "zd_delivery":1,"sum_read":0, "zd_read":1,"sum_down":0, "zd_down":1};
    var fieldArr = ["sum_delivery", "zd_delivery","sum_read", "zd_read","sum_down", "zd_down"];
    /**
     * 根据指标显示日数据
     * @param eventType= 2:第一次加载, 1:切换日，周，月刷新 , 0: 添加对比后又重新选择指标
     */
    function show(businessName,eventType){
        var city_readDown = $("#city_readDown").val();
        var cate_readDown = $("#cate_readDown").val();
        var dateGroup = $("#readDownOption").attr("dateGrp");
        var index_readDown = $("#index_readDown").val();
        myChart.showLoading({ text: '正在努力的读取数据中...' });
        $.ajax({
            url: '/dashboard/resume/ajax/readDownLoad',
            type: 'post',
            async: true,
            data:{"city_readDown":city_readDown,"cate_readDown":cate_readDown,"dateGroup":dateGroup,"index_readDown":index_readDown},
            dataType: 'json',
            success: function(data, textStatus) {
                // //X轴时间
                option_day.xAxis[0].data = data.time;
                option_day.xAxis[1].data = data.time;

                option_day.dataZoom.start = (1-6/data.time.length)*100;
                var datezoom_local={start:(1-6/data.time.length)*100,end:100};
                //小于8个点全显
                if(data.time.length<8){
                    datezoom_local = {start:0,end:100};
                    option_day.dataZoom.start = 0;
                }

                $.each(fieldArr,function(i,f){
                    option_day.series[i].data = data[f];
                });

                if(myChart.component.dataZoom){
                    myChart.component.dataZoom.absoluteZoom(datezoom_local);
                }
                option_day.dataZoom.zoomLock=true;
                myChart.setOption(option_day,true);

                myChart.hideLoading();
            },
            error : function() {
                myChart.hideLoading();
            }
        });
    }

    function getIndexByName(data,name) {
        var dataLength = data.length;
        for (var i = 0; i < dataLength; i++) {
            if (data[i] == name || typeof data[i].value != 'undefined' && data[i].value == name) {
                return i;
            }
        }
        return -1;
    }


    function getAllDays(){
        return option_day.xAxis[0].data;
    }

    $(":button.btn_readDown[dateGrp]").click(function(){
        changeDateGrp(this);
        EC.show();
    });

    //绑定书刷新按钮点击事件
    $("#refresh_readDown_btn").click(function(){
        EC.show();
    });
    
    var EC={
        show:show,
        resize:function(){myChart.resize();},
    };
    return EC;
});

